package com.ruoyi.game.service.impl;

import java.util.List;
import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.game.constants.Constants;
import com.ruoyi.game.domain.GoldCoinRecord;
import com.ruoyi.game.service.IGoldCoinRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.game.mapper.GameUserMapper;
import com.ruoyi.game.api.domin.GameUser;
import com.ruoyi.game.service.IGameUserService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 游戏用户Service业务层处理
 *
 * @author qidian
 * @date 2022-05-06
 */
@Service
public class GameUserServiceImpl implements IGameUserService
{
    @Autowired
    private GameUserMapper gameUserMapper;

    @Autowired
    private IGoldCoinRecordService goldCoinRecordService;

    /**
     * 查询游戏用户
     *
     * @param id 游戏用户主键
     * @return 游戏用户
     */
    @Override
    public GameUser selectGameUserById(Long id)
    {
        return gameUserMapper.selectGameUserById(id);
    }

    /**
     * 查询游戏用户
     *
     * @param userId 用户id
     * @return 游戏用户
     */
    @Override
    public GameUser selectGameUserByUserId(Long userId)
    {
        return gameUserMapper.selectGameUserByUserId(userId);
    }

    /**
     * 查询游戏用户列表
     *
     * @param gameUser 游戏用户
     * @return 游戏用户
     */
    @Override
    public List<GameUser> selectGameUserList(GameUser gameUser)
    {
        return gameUserMapper.selectGameUserList(gameUser);
    }

    /**
     * 新增游戏用户
     *
     * @param gameUser 游戏用户
     * @return 结果
     */
    @Override
    public int insertGameUser(GameUser gameUser)
    {
        gameUser.setCreateTime(DateUtils.getNowDate());
        return gameUserMapper.insertGameUser(gameUser);
    }

    /**
     * 修改游戏用户
     *
     * @param gameUser 游戏用户
     * @return 结果
     */
    @Override
    public int updateGameUser(GameUser gameUser)
    {
        gameUser.setUpdateTime(DateUtils.getNowDate());
        return gameUserMapper.updateGameUser(gameUser);
    }

    /**
     * 批量删除游戏用户
     *
     * @param ids 需要删除的游戏用户主键
     * @return 结果
     */
    @Override
    public int deleteGameUserByIds(Long[] ids)
    {
        return gameUserMapper.deleteGameUserByIds(ids);
    }

    /**
     * 删除游戏用户信息
     *
     * @param id 游戏用户主键
     * @return 结果
     */
    @Override
    public int deleteGameUserById(Long id)
    {
        return gameUserMapper.deleteGameUserById(id);
    }

    /**
     * 充值平台币
     *
     * @param coins 平台币数量
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void recharge(Long coins)
    {
        Long userId = SecurityUtils.getUserId();
        GameUser gameUser = this.selectGameUserByUserId(userId);
        gameUser.setUserCoins(gameUser.getUserCoins() + coins);
        // 添加金币充值记录
        GoldCoinRecord goldCoinRecord = new GoldCoinRecord();
        goldCoinRecord.setRecordType(Constants.GoldCoinRecord.RECHARGE_TYPE);
        goldCoinRecord.setUserId(userId);
        goldCoinRecord.setGoldCoinQuantity(coins.intValue());
        goldCoinRecord.setRemark("充值金币成功");
        goldCoinRecordService.insertGoldCoinRecord(goldCoinRecord);
        this.updateGameUser(gameUser);
    }

    /**
     * 消费平台币
     *
     * @param coins 平台币数量
     * @param remark 备注
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void consumption(Long coins, String remark)
    {
        Long userId = SecurityUtils.getUserId();
        GameUser gameUser = this.selectGameUserByUserId(userId);
        gameUser.setUserCoins(gameUser.getUserCoins() - coins);
        if (gameUser.getUserCoins() < 0)
        {
            throw new RuntimeException("金币数量不足, 请先充值");
        }
        // 添加金币消费记录
        GoldCoinRecord goldCoinRecord = new GoldCoinRecord();
        goldCoinRecord.setRecordType(Constants.GoldCoinRecord.CONSUMPTION_TYPE);
        goldCoinRecord.setUserId(userId);
        goldCoinRecord.setGoldCoinQuantity(coins.intValue());
        goldCoinRecord.setRemark(remark);
        goldCoinRecordService.insertGoldCoinRecord(goldCoinRecord);
        this.updateGameUser(gameUser);
    }

}
